﻿/**
* 
* Copyright (c) 2012 Anders Høst Kjærgaard | ahkj@itu.dk
* 
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction, 
* including without limitation the rights to use, copy, modify, merge, publish, distribute, 
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial 
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Flatland.GeneticProgramming.CoreTypes;

namespace Flatland.AgentBehavior {

    /// <summary>
    /// Base class for the Behavior of an Agent. Chain of responsibility pattern.
    /// </summary>
    public abstract class Behavior : ISerializable {
        //Next behavior in the chain
        protected Behavior Next;

        //The layer this bahavior was attributed to
        public Layer Layer;

        //The agent
        protected Agent Agent;

        public Behavior SetNext(Behavior next) {
            Next = next;
            return Next;
        }

        /// <summary>
        /// Implements execution path for chaining responsibility, but deriving classes may choose to alter this.
        /// </summary>
        public virtual void Respond(ref string msg){
            Act(ref msg);
            if(Next != null)
                Next.Respond(ref msg);
        }

        /// <summary>
        /// Act will be invoked on implementers when it is their responsibility to act.
        /// </summary>
        /// <param name="msg"></param>
        protected abstract void Act(ref string msg);

        /// <summary>
        /// Produces a Behavior for an Agent. 
        /// </summary>
        /// <returns></returns>
        public static Behavior BehaviorFactory(Agent agent, List<Report> genes) {
            if (agent == null) throw new ArgumentNullException("agent");
            if (genes == null) throw new ArgumentNullException("genes");
            if (genes.Count == 0) throw new ArgumentException("Agent Behavior must atleast have one GenoType. Define this as an Layer");
            Behavior behavior = genes[0].GenoType;
            Behavior cur = behavior;
            cur.Agent = agent;
            for (int i = 1; i < genes.Count; i++) {
                cur.Next = genes[i].GenoType;
                cur = cur.Next;
                cur.Agent = agent;
            }
            agent.Reports = genes;
            agent.Behavior = behavior;
            return behavior;
        }

        /// <summary>
        /// Produces a Behavior for an Agent. 
        /// </summary>
        /// <param name="agent"></param>
        /// <param name="report"></param>
        /// <returns></returns>
        public static Behavior BehaviorFactory(Agent agent, Report report) {
            return BehaviorFactory(agent, new List<Report> { report });
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context) { }

        public Behavior(SerializationInfo info, StreamingContext context) { }

        /// <summary>
        /// Public constructor.
        /// </summary>
        public Behavior() { }
    }
}
